﻿#include "prosetpage.h"
#include "ui_prosetpage.h"
#include <QLineEdit>
#include <QDir>
#include <QFileDialog>

ProSetPage::ProSetPage(QWidget *parent)
    : QWizardPage(parent)
    , ui(new Ui::ProSetPage)
{
    ui->setupUi(this);

    registerField("proPath",ui->lineEdit_2);
    registerField("proName*",ui->lineEdit);

    //将两个lineEdit注册为wizard的field，保证两个lineEdit是空的时候无法点击下一步，
    //将QLineEdit的textEdited信号和ProSetPage的completeChanged信号连接起来，这样在lineEdit编辑的时候
    //就会发送textEdited信号，进而触发ProSetPage发送completeChanged信号。
    //completeChanged信号是从proSetPage的基类QWizardPage类继承而来的。
    //completeChanged信号发出后会触发QWizardPage类的isComplete函数。
    connect(ui->lineEdit,&QLineEdit::textEdited,this,&ProSetPage::completeChanged);
    connect(ui->lineEdit_2,&QLineEdit::textEdited,this,&ProSetPage::completeChanged);
    QString curPath = QDir::currentPath();
    ui->lineEdit_2->setText(curPath);
    ui->lineEdit_2->setCursorPosition(ui->lineEdit_2->text().size());
    //setClearButtonEnabled设置为true可以在lineEdit输入数据后显示清除按钮，直接清除已录入的字符。
    ui->lineEdit->setClearButtonEnabled(true);
    ui->lineEdit_2->setClearButtonEnabled(true);

}

ProSetPage::~ProSetPage()
{
    delete ui;
}

void ProSetPage::GetProSettings(QString &name, QString &path)
{
    name = ui->lineEdit->text();
    path = ui->lineEdit_2->text();
}

//重写isComplete函数。这样我们就能判断文件夹是否合理以及是否已经有项目路径了。
//可以根据不满足的条件设置tips提示用户。
bool ProSetPage::isComplete() const
{
    if(ui->lineEdit->text() == "" || ui->lineEdit_2->text() == "")
    {
        return false;
    }

    QDir dir(ui->lineEdit_2->text());
    if(!dir.exists())
    {
        ui->tips->setText("project path is not exists");
        return false;
    }

    //判断路径
    QString absFilePath = dir.absoluteFilePath(ui->lineEdit->text());
    QDir dist_dir(absFilePath);
    if(dist_dir.exists())
    {
        ui->tips->setText("project has exists,change path or name!");
        return false;
    }

    ui->tips->setText("");
    return QWizardPage::isComplete();

}

//为浏览按钮添加点击后选择文件夹操作
void ProSetPage::on_pushButton_clicked()
{
    QFileDialog file_dialog;
    file_dialog.setFileMode(QFileDialog::Directory);
    file_dialog.setWindowTitle(u8"选择导入的文件夹");
    auto path = QDir::currentPath();
    file_dialog.setDirectory(path);
    file_dialog.setViewMode(QFileDialog::Detail);

    QStringList fileNames;
    if(file_dialog.exec())
    {
        fileNames = file_dialog.selectedFiles();
    }

    if(fileNames.length() <= 0)
    {
        return;
    }

    //获取导入的路径
    QString import_path = fileNames.at(0);
    ui->lineEdit_2->setText(import_path);
}














